package pub.guoxin.oops.manager.controller;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import pub.guoxin.oops.annotation.InjectUser;
import pub.guoxin.oops.constants.OopsPathConstants;
import pub.guoxin.oops.manager.service.DataAlarmConfigService;
import pub.guoxin.oops.manager.vo.DataAlarmConfigVo;
import pub.guoxin.oops.model.DataAlarmConfig;
import pub.guoxin.oops.model.User;
import pub.guoxin.oops.vo.ResultVo;
import pub.guoxin.oops.vo.ResultVoBuilder;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Create by guoxin on 2018/4/15
 */
@RestController
@RequestMapping(OopsPathConstants.DATA_ALARM_CONFIG_PATH)
public class DataAlarmConfigController {

    @Autowired
    private DataAlarmConfigService dataAlarmConfigService;
    @Autowired
    private ResultVoBuilder builder;

    @ApiOperation(value = "初始化边界值", notes = "初始化边界值")
    @PostMapping(value = "/init", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultVo init(@InjectUser User user) {
        dataAlarmConfigService.init(user.getCompanyId());
        return builder.buildSuccessResultInfo();
    }

    @ApiOperation(value = "修改边界值", notes = "修改边界值")
    @PutMapping(value = "/", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultVo<List<DataAlarmConfig>> modify(@Valid @RequestBody List<DataAlarmConfigVo> configs) {
        List<DataAlarmConfig> collect = configs.stream().map(DataAlarmConfigVo::createDataAlarmConfig).collect(Collectors.toList());
        List<DataAlarmConfig> modify = dataAlarmConfigService.modify(collect);
        return builder.buildSuccessResultInfo(modify);
    }

    @ApiOperation(value = "查询当前用户下的所有边界值", notes = "查询当前用户下的所有边界值")
    @GetMapping(value = "/", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultVo<List<DataAlarmConfig>> all(@InjectUser User user) {
        List<DataAlarmConfig> all = dataAlarmConfigService.all(user.getCompanyId());
        return builder.buildSuccessResultInfo(all);
    }

}
